<template>
	<div class="chatroom-list">
		<div class="list-search">
			<el-input placeholder="Search"></el-input>
			<el-dropdown @command="handleCommand">
				<el-button>
					<el-icon>
						<Plus />
					</el-icon>
				</el-button>
				<template #dropdown>
					<el-dropdown-menu>
						<el-dropdown-item command="new-room">New Room</el-dropdown-item>
					</el-dropdown-menu>
				</template>
			</el-dropdown>
		</div>
		<el-card v-for="room in chatRooms" :key="room.id" class="chat-room-card" @click="selectRoom(room)">
			<div class="chat-room-info">
				<div class="chat-room-name" :class="{
					selected: room.id === currentChatRoom
				}">{{ room.name }}
				</div>
				<div class="chat-room-description">{{ room.desc }}</div>
			</div>
		</el-card>
	</div>
</template>

<script>
import {
	ref,
} from 'vue';
import { useRouter } from "vue-router";
import {Plus} from "@element-plus/icons-vue";

export default {
	name: 'ChatRoomList',
	components: {Plus},
	setup() {
		const router = useRouter();
		const chatRooms = ref([]);
		const currentChatRoom = ref(null);

		const selectChatRoom = (room) => {
			console.log(room.id)
			currentChatRoom.value = room.id;
			router.push({
				name: 'chat',
				params: {
					id: room.id
				}
			});
		};

		return {
			chatRooms,
			selectChatRoom,
			currentChatRoom
		};
	},
	methods: {
		selectRoom(room) {
			this.selectChatRoom(room);
			this.$emit('selectChatRoom', room);
		},

		async getChatRooms() {
			const res = await this.$api.getChatRooms();

			this.chatRooms = res.data.data;
		},

		handleCommand(command) {
			if (command === 'new-room') {
				this.createChatRoom();
			} else if (command === 'new-ai-chat') {
				this.createAIChat();
			}
		},
		createChatRoom() {
			// 弹出对话框让用户输入聊天室名称和描述
			this.$prompt('请输入聊天室名称和描述，用逗号分隔', '添加聊天室', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				inputPattern: /.+,.+/,
				inputErrorMessage: '格式错误，正确格式：名称,描述'
			}).then(({ value }) => {
				const [name, description] = value.split(',');
				this.$api.createChatRoom({ name, description }).then(() => {
					this.getChatRooms(); // 重新获取聊天室列表
				});
			}).catch(() => { });
		},
		createAIChat() {
			// 创建与AI的对话
			this.$api.createChatRoom({ name: 'AI对话', description: '与AI的私人对话' }).then(() => {
				this.getChatRooms(); // 重新获取聊天室列表
			});
		},

		handleCommand(command) {
			if (command === 'new-room') {
				this.createChatRoom();
			} else if (command === 'new-ai-chat') {
				this.createAIChat();
			}
		},
		createChatRoom() {
			// 弹出对话框让用户输入聊天室名称和描述
			this.$prompt('请输入聊天室名称和描述，用逗号分隔', '添加聊天室', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				inputPattern: /.+,.+/,
				inputErrorMessage: '格式错误，正确格式：名称,描述'
			}).then(({ value }) => {
				const [name, message] = value.split(',');
				this.$api.createChatRoom({ name, message }).then(() => {
					this.getChatRooms(); // 重新获取聊天室列表
				});
			}).catch(() => { });
		},
	},

	mounted() {
		this.getChatRooms();
	},
};
</script>

<style scoped>
* {
	margin: 0;
	padding: 0;
	user-select: none;
}

.chatroom-list {
	display: flex;
	flex-direction: column;
}

.list-search {
	height: 65px;
	display: flex;
	justify-content: center;
	align-items: center;
	padding-left: 6px;
	padding-right: 6px;

}

.list-search .el-input {
	flex-grow: 1;
	margin-right: 4px;
}

.list-search .el-button {
	width: 40px;
	height: 32px;
}

.list-search .el-button .el-icon {
	font-size: 16px;
}

.chat-room-card {
	border: none;
	box-shadow: none;
	border-radius: 0;
	cursor: pointer;
	background-color: transparent;
}

.chat-room-card:hover {
	background-color: rgba(0, 0, 0, 0.1);
}

.chat-room-info {
	display: flex;
	flex-direction: column;
}

.chat-room-name {
	transition: color 0.3s;
}

.chat-room-name.selected {
	color: #409EFF;
}

.chat-room-description {
	font-size: 0.5rem;
	color: #666;
}
</style>